import 'package:extended_image/extended_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_application_1/components/choose_bottom_sheet/choose_bottom_sheet.dart';
import 'package:flutter_application_1/utils/file_utils.dart';
import 'package:fluttertoast/fluttertoast.dart';

class PreviewImages {
  // 长按
  static onLongPress({
    required BuildContext context,
    required String url,
  }) {
    showChooseBottomSheet(
      context: context,
      sheets: [
        ChooseButton(
          text: '保存到相册',
          onTap: () async {
            await FileUtils.saveFile(
              filePath: url,
            );
            Fluttertoast.showToast(msg: '保存成功');
            Navigator.pop(context);
          },
        ),
      ],
    );
  }

  // 预览
  static preview({
    required BuildContext context,
    required List<String> urls,
    required int currentIndex,
  }) {
    if (urls.length == 0) return;

    Navigator.push(
      context,
      PageRouteBuilder(
        // 页面生成
        pageBuilder: (BuildContext context, _, __) {
          return AnnotatedRegion<SystemUiOverlayStyle>(
            value: SystemUiOverlayStyle(
              statusBarIconBrightness: Brightness.light,
            ),
            child: Theme(
              data: ThemeData(
                accentColor: Colors.white,
              ),
              child: Scaffold(
                backgroundColor: Colors.black,
                // PageView
                body: ExtendedImageGesturePageView.builder(
                  itemBuilder: (BuildContext context, int index) {
                    String url = urls[index];

                    Widget image = GestureDetector(
                      // 长按
                      onLongPress: () {
                        onLongPress(
                          context: context,
                          url: url,
                        );
                      },
                      child: ExtendedImage.network(
                        url,
                        fit: BoxFit.contain,
                        mode: ExtendedImageMode.gesture,
                        initGestureConfigHandler: (_) {
                          return GestureConfig(
                            inPageView: true,
                            initialScale: 0.95,
                          );
                        },
                      ),
                    );

                    if (index == currentIndex) {
                      return Hero(
                        tag: url + index.toString(),
                        child: image,
                      );
                    } else {
                      return image;
                    }
                  },
                  itemCount: urls.length,
                  controller: PageController(
                    initialPage: currentIndex,
                  ),
                ),
              ),
            ),
          );
        },
        // 自定义动画
        transitionsBuilder: (
          BuildContext context,
          Animation<double> animation,
          Animation<double> secondaryAnimation,
          Widget child,
        ) {
          return FadeTransition(
            opacity: animation,
            child: child,
          );
        },
      ),
    );
  }
}
